package com.web.controller.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.web.dao.RechargeRecordMapper;
import com.web.domain.dataTable.BootstrapTableColumns;
import com.web.domain.dataTable.BootstrapTableResponse;
import com.web.entity.RechargeRecord;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author FDjavaone
 * @date 2017/10/17
 */
@Controller
public class WebRechargeController {
    @Autowired
    private RechargeRecordMapper rechargeRecordMapper;


    @RequestMapping(value = "sysf/recharge",
            method = RequestMethod.GET)
    public String recharge(Model model, HttpServletRequest request) {
        return "rechargeRecord";
    }

    @Data
    public static class RechargeColumns extends BootstrapTableColumns {
        private String outTradeNo;
        private String name;
        private String phone;
        private Integer type;
        private Integer status;
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date begin_createTime;
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date end_createTime;
    }

    @RequestMapping("sysf/getRecharges")
    @ResponseBody
    public BootstrapTableResponse getOrders(RechargeColumns rechargeColumns) {

        if (StringUtils.isNotBlank(rechargeColumns.getOutTradeNo())) {
            rechargeColumns.setOutTradeNo("%" + rechargeColumns.getOutTradeNo() + "%");
        }
        if (StringUtils.isNotBlank(rechargeColumns.getName())) {
            rechargeColumns.setName("%" + rechargeColumns.getName() + "%");
        }
        if (StringUtils.isNotBlank(rechargeColumns.getPhone())) {
            rechargeColumns.setPhone("%" + rechargeColumns.getPhone() + "%");
        }
        PageHelper.startPage(rechargeColumns.getPageNumber(), rechargeColumns.getPageSize());
        Page<RechargeRecord> page = rechargeRecordMapper.selectAllRechages(rechargeColumns);
        BootstrapTableResponse bootstrapTableResponse = new BootstrapTableResponse();
        bootstrapTableResponse.setResult(page.getResult());
        bootstrapTableResponse.setTotalCount(page.getTotal());
        return bootstrapTableResponse;
    }

}
